﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace InnerProduct
{
    public partial class Slider : UserControl
    {
        bool drag = false;
        public Slider()
        {
            InitializeComponent();
            MouseLeftButtonDown += new MouseButtonEventHandler(Slider_MouseLeftButtonDown);
            MouseLeftButtonUp += new MouseButtonEventHandler(Slider_MouseLeftButtonUp);
            MouseMove += new MouseEventHandler(Slider_MouseMove);
            LostMouseCapture += new MouseEventHandler(Slider_LostMouseCapture);
            KeyDown += new KeyEventHandler(Slider_KeyDown);
            SizeChanged += new SizeChangedEventHandler(Slider_SizeChanged);
        }

        void Slider_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Thumb.Margin = new Thickness(0, Layout.RowDefinitions[1].ActualHeight * (1 - Color.V), 0, 0);
        }

        void Slider_KeyDown(object sender, KeyEventArgs e)
        {
            double v = Color.V;
            if (e.Key == Key.Up)
            {
                v += 0.1;
                e.Handled = true;
            }
            else if (e.Key == Key.Down)
            {
                v -= 0.1;
                e.Handled = true;
            }
            SetV(v);
        }

        private void SetV(double v)
        {
            v = Math.Max(0, v);
            v = Math.Min(1, v);
            ColorHSV nu = Color.Clone();
            nu.V = v;
            Color = nu;
        }

        void Slider_MouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                Point pt = e.GetPosition(Layout);
                ColorFromPoint(pt);
            }
        }

        void Slider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            drag = false;
        }

        void Slider_LostMouseCapture(object sender, MouseEventArgs e)
        {
            drag = false;
        }

        void Slider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Focus();
            if (CaptureMouse())
                drag = true;
            Point pt = e.GetPosition(Layout);
            ColorFromPoint(pt);
        }

        private void ColorFromPoint(Point pt)
        {
            double R1Height = Layout.RowDefinitions[0].ActualHeight;
            double v = 1 - (pt.Y - R1Height) / Layout.RowDefinitions[1].ActualHeight;
            SetV(v);
        }

        #region Color (DependencyProperty)

        /// <summary>
        /// HSV Color
        /// </summary>
        public ColorHSV Color
        {
            get { return (ColorHSV)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }
        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color", typeof(ColorHSV), typeof(Slider),
            new PropertyMetadata(new ColorHSV(), new PropertyChangedCallback(OnColorChanged)));

        private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Slider)d).OnColorChanged(e);
        }

        protected virtual void OnColorChanged(DependencyPropertyChangedEventArgs e)
        {
            ColorHSV c = ((ColorHSV)e.NewValue).Clone();
            c.A = 255;
            Tab.Fill = new SolidColorBrush(c.Color);
            Thumb.Margin = new Thickness(0.0, Layout.RowDefinitions[1].ActualHeight * (1 - c.V), 0, 0);
            c.V = 1;
            GStop.Color = c.Color;
        }

        #endregion

    }
}
